package com.syr.mybatis.policy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syr.mybatis.QueryCompare;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunyiran
 * @since 2021/1/14
 */
@Slf4j
public class RightLikeComparePolicy implements QueryComparePolicy {

    @Override
    public <E, P> void addCompare(QueryWrapper<E> queryWrapper, QueryCompare queryCompare, P p) {
        try {
            Object v = queryCompare.getField().get(p);
            if (v != null || queryCompare.isNeedNull()) {
                queryWrapper.likeRight(queryCompare.getColumnName(), v);
            }
        } catch (IllegalAccessException e) {
            log.error("error to add Compare for {}", p.getClass().getSimpleName(), e);
            throw new RuntimeException(e.getMessage());
        }
    }
}
